package teach.day05;

import java.util.*;
//Collections类的方法使用
public class P07CollectionsDemo {
    public static void main(String[] args) {
        System.out.println(Math.sqrt(2));
        Math math = null;
        System.out.println(math);
        System.out.println(math.sqrt(2));
        List<Integer> list = new Vector<>();
        Random r = new Random();
        for (int i = 0; i < 10;i++) {
            list.add(r.nextInt(100));
        }
        System.out.println(list);
        //最大最小
        System.out.println(Collections.max(list));  //max
        System.out.println(Collections.min(list));  //min
        //默认排序、二分查找
        Collections.sort(list);     //升序-非降序
        System.out.println(list);
        System.out.println(Collections.binarySearch(list, 8));
        //降序、二分查找
        Collections.sort(list, Collections.reverseOrder()); //非增序
        System.out.println(list);
        System.out.println(Collections.binarySearch(list, 8,Collections.reverseOrder()));
        //多关键字查找
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                int cmp =  hash(o1) - hash(o2) ;        //按照hash值从小到大排序
                if (cmp != 0) return cmp;
                return o1 - o2;
            }
            int hash(int x){        //计算一个整数的各位数字之和
                int result = 0;
                while (x > 0) {
                    result += x % 10;
                    x /= 10;
                }
                return result;
            }
        });
        System.out.println(list);

        //洗牌截取子序算法--乱序
        Collections.shuffle(list);
        System.out.println(list);
    }
}
